# This is a sample Makefile which compiles source files named:
# - tcpechotimeserv.c
# - tcpechotimecliv.c
# - time_cli.c
# - echo_cli.c
# and creating executables: "server", "client", "time_cli"
# and "echo_cli", respectively.
#
# It uses various standard libraries, and the copy of Stevens'
# library "libunp.a" in ~cse533/Stevens/unpv13e_solaris2.10 .
#
# It also picks up the thread-safe version of "readline.c"
# from Stevens' directory "threads" and uses it when building
# the executable "server".
#
# It is set up, for illustrative purposes, to enable you to use
# the Stevens code in the ~cse533/Stevens/unpv13e_solaris2.10/lib
# subdirectory (where, for example, the file "unp.h" is located)
# without your needing to maintain your own, local copies of that
# code, and without your needing to include such code in the
# submissions of your assignments.
#
# Modify it as needed, and include it with your submission.

CC = gcc

#UNP_LIB_PATH = /home/courses/cse533/Stevens/unpv13e_solaris2.10
UNP_LIB_PATH = unpv13e_linux

#LIBS = -lresolv -lsocket -lnsl -lpthread\
#	$(UNP_LIB_PATH)/libunp.a\

LIBS = -lresolv -lnsl -lpthread\
	$(UNP_LIB_PATH)/libunp.a\
	
FLAGS = -g -O2

CFLAGS = ${FLAGS} -I$(UNP_LIB_PATH)/lib

all: client server echo_cli time_cli 

time_cli: time_cli.o
	${CC} ${FLAGS} -o time_cli time_cli.o ${LIBS}
time_cli.o: time_cli.c
	${CC} ${CFLAGS} -c time_cli.c


echo_cli: echo_cli.o str_cli_ext.o 
	${CC} ${FLAGS} -o echo_cli echo_cli.o str_cli_ext.o ${LIBS}
echo_cli.o: echo_cli.c
	${CC} ${CFLAGS} -c echo_cli.c
str_cli_ext.o: str_cli_ext.c
	${CC} ${CFLAGS} -c str_cli_ext.c

# server uses the thread-safe version of readline.c

tcpechotimesrv.o: tcpechotimesrv.c
	${CC} ${CFLAGS} -c tcpechotimesrv.c
str_echo_ext.o: str_echo_ext.c
	${CC} ${CFLAGS} -c str_echo_ext.c
writen.o: writen.c
	${CC} ${CFLAGS} -c writen.c
tcpechotimesrvlib.o: tcpechotimesrvlib.c
	${CC} ${CFLAGS} -c tcpechotimesrvlib.c

server: tcpechotimesrv.o readline.o str_echo_ext.o tcpechotimesrvlib.o writen.o\
tcp_listen.o
	${CC} ${FLAGS} -o server tcpechotimesrv.o readline.o str_echo_ext.o\
    tcpechotimesrvlib.o writen.o tcp_listen.o ${LIBS}

client: tcpechotimecli.o get_ip_addr.o sigchldwaitpid.o tcpechotimeclilib.o 
	${CC} ${FLAGS} -o client tcpechotimecli.o get_ip_addr.o sigchldwaitpid.o\
    tcpechotimeclilib.o ${LIBS}

tcpechotimecli.o: tcpechotimecli.c
	${CC} ${CFLAGS} -c tcpechotimecli.c
get_ip_addr.o: get_ip_addr.c
	${CC} ${CFLAGS} -c get_ip_addr.c
sigchldwaitpid.o: sigchldwaitpid.c
	${CC} ${CFLAGS} -c sigchldwaitpid.c
tcpechotimeclilib.o: tcpechotimeclilib.c
	${CC} ${CFLAGS} -c tcpechotimeclilib.c
tcp_listen.o: tcp_listen.c
	${CC} ${CFLAGS} -c tcp_listen.c

# pick up the thread-safe version of readline.c from directory "threads"

readline.o: readline.c
	${CC} ${CFLAGS} -c readline.c

clean:
	rm echo_cli echo_cli.o server tcpechotimesrv.o client tcpechotimecli.o\
    time_cli time_cli.o readline.o str_echo_ext.o get_ip_addr.o str_cli_ext.o\
    sigchldwaitpid.o tcpechotimeclilib.o tcpechotimesrvlib.o writen.o\
    tcp_listen.o

test: test_client_args test_echo_client

test_client_args: test_client_args.o get_ip_addr.o
	${CC} ${FLAGS} -o test_client_args test_client_args.o get_ip_addr.o ${LIBS}

test_echo_client: test_echo_client.o tcpechotimeclilib.o sigchldwaitpid.o\
get_ip_addr.o
	${CC} ${FLAGS} -o test_echo_client test_echo_client.o tcpechotimeclilib.o\
    sigchldwaitpid.o get_ip_addr.o ${LIBS}

test_client_args.o: test_client_args.c
	${CC} ${CFLAGS} -c test_client_args.c

test_echo_client.o: test_echo_client.c
	${CC} ${CFLAGS} -c test_echo_client.c

test_clean:
	rm -f test_client_args.o test_echo_client.o test

